package com.leon.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.leon.api.service.UserService;
import com.leon.api.controller.UserControllerApi;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @description:
 * @program: heima-wallet
 * @author: Leon
 * @date: 2021-03-08 20:21
 **/
@RestController
@RequestMapping("/user")
public class UserController implements UserControllerApi {

    @Reference(version = "1.0.0")
    private UserService userService;


    @Override
    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestBody Map<String, String> param) {
        String phone = param.get("phone");
        String code = param.get("code");
        String token = userService.login(phone, code);

        if (StringUtils.isEmpty(token)){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }

        return ResponseEntity.ok(token);
    }

    @Override
    @PostMapping("/register")
    public ResponseEntity<Object> register(@RequestBody Map<String, String> param) {
        String phone = param.get("phone");
        String code = param.get("code");
        String password = param.get("password");
        String username = param.get("username");
        Boolean register = userService.register(username, phone, password, code);
        if (!register){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
        return ResponseEntity.ok(null);
    }

    @Override
    @PostMapping("/getCode")
    public Boolean getCode(@RequestBody Map<String,String> param) {
        String phone = param.get("phone");
        String code = userService.getCode(phone);
        if (StringUtils.isEmpty(code)){
            return false;
        }
        return true;
    }
}
